﻿package _4_ListAndComboBox;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.Locale;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 * 2011-6-5 0:33:50
 * @author Administrator
 */
public class TJComboBox {
    JFrame frame;
    Container contentPane;
    JComboBox<Integer> comboBox;
    JLabel label;
    public TJComboBox(){
        frame=new JFrame("ComboBox测试");
        contentPane=frame.getContentPane();
        Box box=Box.createVerticalBox();
        Box hBox;
        setLabel();
        box.add(Box.createVerticalStrut(10));
        hBox=Box.createHorizontalBox();
        hBox.add(Box.createHorizontalGlue());
        hBox.add(label);
        hBox.add(Box.createHorizontalGlue());
        box.add(hBox);
        box.add(Box.createVerticalStrut(10));
        setComboBox();
        hBox=Box.createHorizontalBox();
        hBox.add(Box.createHorizontalGlue());
        hBox.add(comboBox);
        hBox.add(Box.createHorizontalGlue());
        box.add(hBox);
        box.add(Box.createVerticalStrut(10));
        contentPane.add(box);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void setLabel(){
        label=new JLabel();
        label.setText("Swing 目前字体大小:"+label.getFont().getSize());
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        label.setBackground(Color.red);
        label.setForeground(Color.green);
        label.setOpaque(true);
        Font font=new Font("楷体",Font.PLAIN,100);
//        label.setFont(font);
        String str=new String("Swing 目前字体大小:100");
        Rectangle2D rect=font.getStringBounds(str, new FontRenderContext(new AffineTransform(), true, true));
        label.setPreferredSize(new Dimension((int)rect.getWidth(),(int)rect.getHeight()));
        label.setMinimumSize(new Dimension((int)rect.getWidth(),(int)rect.getHeight()));
        label.setSize(new Dimension((int)rect.getWidth(),(int)rect.getHeight()));
    }
    public void setComboBox(){
        Integer size[]={10,11,12,13,14,15,20,30,40,50};
        DefaultComboBoxModel<Integer> model=new DefaultComboBoxModel<>(size);
        comboBox=new JComboBox<Integer>(model);
        comboBox.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e) {
                if(comboBox.getSelectedIndex() == -1) {
                    return;
                }
                if(e.getStateChange() == ItemEvent.DESELECTED) {
                    return;
                }
                int size=(Integer)comboBox.getSelectedItem();
                label.setText("Swing 目前字体大小:"+size);
                label.setFont(new Font(label.getFont().getName(),label.getFont().getStyle(),size));
                System.out.println(String.format(Locale.getDefault(), 
                        "itemStateChanged, idx:%d, state:%s", 
                        comboBox.getSelectedIndex(), e.paramString()));
            }
        });
        comboBox.setSelectedIndex(0);
    }
    public static void main(String args[]){
        new TJComboBox();
    }
}
